package guowei.guliuser.user.controller;

import java.util.Arrays;
import java.util.Map;

import guowei.common.exception.BizCodeEnume;
import guowei.guliuser.user.exception.PhoneNotOnlyOneException;
import guowei.guliuser.user.exception.UserNameNotOnlyOneException;
import guowei.guliuser.user.vo.LoginVoUser;
import guowei.guliuser.user.vo.RegMemVo;
import guowei.guliuser.user.vo.WeiBoToken;
import guowei.guliuser.user.vo.WeiboUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import guowei.guliuser.user.entity.MemberEntity;
import guowei.guliuser.user.service.MemberService;
import guowei.common.utils.PageUtils;
import guowei.common.utils.R;



/**
 * 会员
 *
 * @author guoguo
 * @email 1573746397@qq.com
 * @date 2023-05-21 12:30:01
 */
@RestController
@RequestMapping("user/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("user:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("user:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存/
     */
    @RequestMapping("/save")
    //@RequiresPermissions("user:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("user:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("user:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 保存注册的会员信息   若有用户不唯一或手机号不唯一会抛出指定异常
     * @param regMemVo
     * @return
     */
    @PostMapping("/registry")
    public R regMem(@RequestBody RegMemVo regMemVo){
        try {
            memberService.saveRegMem(regMemVo);
        } catch (UserNameNotOnlyOneException e) {
            return R.error(BizCodeEnume.USERNAMENOTONLY_EXCEPTION.getCode(), BizCodeEnume.USERNAMENOTONLY_EXCEPTION.getMsg());
        } catch (PhoneNotOnlyOneException e){
            return R.error(BizCodeEnume.PHONENOTONLY_EXCEPTION.getCode(), BizCodeEnume.PHONENOTONLY_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    @PostMapping("/login")
    public R LoginUser(@RequestBody LoginVoUser vo){
        R r = memberService.loginVo(vo);
        return r;
    }

    @PostMapping("/weibo/user/save")
    public R saveWeiboUser(@RequestBody WeiBoToken weiBoToken){
        R r = memberService.saveWeiboUser(weiBoToken);
        return r;
    }

}
